<?php
/**
 * 轻舟CMS(QZLOG)
 *
 * 一款基于PHP+MYSQL的开源内容管理系统，模板标签简单、可扩展性强，利于二次开发，轻松建站的首选利器。
 *
 * @package     QZLOG
 * @license     http://www.qzlog.com/license
 * @link        http://www.qzlog.com
 * @mail        info@shyohan.com
 * @since       Version 3.0
 * @copyright   Copyright (c) 2015 - 2017 上海优翰信息科技有限公司. All rights reserved.
 */

defined('IN_QZLOG') OR exit('QZLOG:No direct script access allowed!');

/**
 * 菜单管理模型
 * @author LINK <link@shyohan.com>
 */
class MenuModel extends ViewModel
{
    public $table = "menu";

    //表单验证
    public $validate = array(
        array('title', 'nonull', '标题不能为空', 2, 1),
        array('url', 'nonull', '链接不能为空', 2, 1)
    );

    /**
     * 添加菜单
     */
    public function addMenu()
    {
        if ($this->create()) {
//            $map['url'] = array('EQ', $this->data['url']);
//            if (M('menu')->where($map)->find()) {
//                $this->error = '链接已存在';
//                return false;
//            }
            if (empty($this->data['icon'])) $this->data['icon'] = '&#xe626;';
            if ($this->add()) {
                return true;
            } else {
                $this->error = '添加失败';
                return false;
            }
        }
    }

    /**
     * 修改菜单
     */
    public function editMenu()
    {
        if ($this->create()) {
            if ($this->save()) {
                return true;
            } else {
                $this->error = '修改失败';
            }
        }
    }

    //删除菜单
    public function delMenu()
    {
        $id = Q("id", 0);
        if (!$id) {
            $this->error = '参数错误';
            return false;
        }
        $map['pid'] = array('in', explode(",", $id));
        $state = $this->where($map)->all();
        if ($state) {
            $this->error = '请删除子菜单';
            return false;
        }
        $map2['id'] = array('in', explode(",", $id));
        if ($this->where($map2)->del()) {
            return true;
        } else {
            $this->error = '删除失败';
        }
    }

    //改变菜单状态
    public function changeStatusMenu()
    {
        $id = Q("id", 0);
        $method = Q("method");
        if (!$id) {
            $this->error = '请选择要操作的数据';
            return false;
        }
        if (in_array(1, explode(',', $id))) {
            $this->error = '不允许对后台默认节点执行该操作';
            return false;
        }
        switch (strtolower($method)) {
            case 'forbid':
                if ($this->where(array('id' => array('in', explode(",", $id))))->save(array('hide' => 1))) {
                    return true;
                } else {
                    $this->error = '隐藏失败!';
                    return false;
                }
                break;
            case 'resume':
                if ($this->where(array('id' => array('in', explode(",", $id))))->save(array('hide' => 0))) {
                    return true;
                } else {
                    $this->error = '恢复失败!';
                    return false;
                }
                break;
            default:
                $this->error = '参数非法';
                return false;
        }

    }

    /**
     * 获取所有菜单id=>title形式
     * @author Link <612012@qq.com>
     * @return array
     */
    function getMenusIdTitle()
    {
        $result = M('menu')->all();
        $data = array();
        foreach ($result as $val) {
            $data[$val['id']] = $val['title'];
        }
        return $data;
    }
}